import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yinshui_caiying/widgets/border_dashed.dart';

///单元格带输入框小组件
///
///[title]左侧标题,*必填
///
///[placeholder]输入提示
///
///[controller]输入框控制器，当初始化时要反填值时用
///
///[onChanged]值改变的回调函数
///
///[inputFormatters]格式化函数
///
///[keyboardType]软件盘弹起的类型
///
///[unit]输入框后面的单位，比如 m、元
///
///[request]是否必填，是的话表单验证时会以弹窗的形式提示placeholder的内容
///
///[validator]验证规则，想用自定义验证规则的话，就传入
///[hasBorder]是否展示下划线，默认true
class FormInput extends StatelessWidget {
  String title;
  String placeholder;
  TextEditingController? controller;
  void Function(String)? onChanged;
  List<TextInputFormatter>? inputFormatters;
  TextInputType? keyboardType;
  String unit;
  FocusNode focusNode = FocusNode();
  final textLength = 0.obs;
  String? Function(String?)? validator;
  bool hasBorder;
  String? initValue;
  bool request;
  FormInput(
      {required this.title,
      this.controller,
      this.onChanged,
      this.inputFormatters,
      this.keyboardType,
      this.unit = '',
      this.placeholder = '请输入内容',
      this.validator,
      this.initValue,
      this.hasBorder = true,
      this.request = false});

  @override
  Widget build(BuildContext context) {
    if (controller == null) {
      controller = TextEditingController.fromValue(TextEditingValue(
          text: initValue != null ? initValue! : '',
          selection: TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream, offset: textLength.value))));
    }
    textLength.value = controller!.text.length;
    if (!controller!.hasListeners) {
      controller!.addListener(() {
        textLength.value = controller!.text.length;
      });
    }
    if (!focusNode.hasListeners) {
      focusNode.addListener(() {
        if (focusNode.hasFocus) {
          //有焦点
          controller!.selection = TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream, offset: textLength.value));
        } else {
          //失去焦点
        }
      });
    }

    return GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(focusNode);
        },
        child: Column(children: [
          Container(
              height: 147.h,
              child: Obx(() => Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text(
                          title,
                          style: TextStyle(
                            fontSize: 40.sp,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                        Expanded(
                            flex: 1,
                            child: Container(
                              alignment: Alignment.centerRight,
                              height: 147.h,
                              child: TextFormField(
                                  focusNode: focusNode,
                                  keyboardType: keyboardType,
                                  inputFormatters: inputFormatters,
                                  onChanged: onChanged,
                                  controller: controller,
                                  // obscureText: isPassword,
                                  textAlign: TextAlign.right,
                                  textAlignVertical: TextAlignVertical.top,
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 40.sp,
                                    fontWeight: FontWeight.w400,
                                  ),
                                  decoration: InputDecoration(
                                      hintText: placeholder,
                                      hintStyle: TextStyle(
                                          color:
                                              Color.fromRGBO(193, 192, 192, 1)),
                                      border: InputBorder.none,
                                      isDense: true,
                                      errorStyle: TextStyle(height: 0)),
                                  validator: this.request
                                      ? (value) {
                                          if (value == '') {
                                            SmartDialog.showToast(
                                                this.placeholder,
                                                alignment: Alignment.center);
                                            return '';
                                          } else {
                                            return null;
                                          }
                                        }
                                      : null),
                            )),
                        textLength.value > 0
                            ? Text(
                                unit,
                                style: TextStyle(height: 3.h, fontSize: 36.sp),
                              )
                            : Container()
                      ]))),
          hasBorder ? MySeparator() : Container()
        ]));
  }
}
